<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SpringBoot 启动流程深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
        }
        .code-block pre {
            margin: 0;
            padding: 1.5rem;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.875rem;
            line-height: 1.6;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
        }
        .section-divider {
            position: relative;
            text-align: center;
            margin: 3rem 0;
        }
        .section-divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
        }
        .section-divider span {
            background: white;
            padding: 0 2rem;
            position: relative;
            color: #9ca3af;
            font-size: 0.875rem;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .highlight-box {
            background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
            border-left: 4px solid #667eea;
            padding: 1.5rem;
            margin: 1.5rem 0;
            border-radius: 0 8px 8px 0;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                SpringBoot 启动流程
                <span class="block text-3xl md:text-4xl mt-4 font-normal opacity-90">深度解析与源码剖析</span>
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl leading-relaxed">
                <i class="fas fa-rocket mr-2"></i>
                探索 SpringApplication 的核心机制，理解 Spring Boot 如何简化应用启动过程，掌握从零到一的完整启动链路。
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <section class="bg-white rounded-2xl shadow-lg p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap serif-font">S</span>pringApplication 是 Spring Boot 的核心类之一，用于启动 Spring 应用程序。它封装了应用的启动过程，包括创建应用上下文、启动自动配置、扫描组件、以及运行嵌入式 Web 服务器等一系列操作。Spring Boot 的启动流程是其简化开发体验的重要组成部分。
            </p>
        </section>

        <!-- SpringApplication Overview -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <i class="fas fa-cube mr-3 text-purple-600"></i>
                <span class="gradient-text">SpringApplication 类概述</span>
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-cogs mr-2 text-purple-500"></i>核心职责
                    </h3>
                    <p class="text-gray-600 leading-relaxed">
                        SpringApplication 类的核心职责是启动 Spring 应用并配置应用上下文。它通过封装复杂的启动逻辑，让开发者能够通过简单的 run() 方法启动整个应用。
                    </p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-tasks mr-2 text-purple-500"></i>主要功能
                    </h3>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-check-circle mr-2 text-green-500"></i>加载 Spring 配置文件</li>
                        <li><i class="fas fa-check-circle mr-2 text-green-500"></i>创建 Spring 上下文</li>
                        <li><i class="fas fa-check-circle mr-2 text-green-500"></i>启动自动配置机制</li>
                        <li><i class="fas fa-check-circle mr-2 text-green-500"></i>执行容器启动</li>
                        <li><i class="fas fa-check-circle mr-2 text-green-500"></i>配置嵌入式 Web 服务器</li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Constructor Analysis -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <i class="fas fa-hammer mr-3 text-purple-600"></i>
                <span class="gradient-text">关键构造方法解析</span>
            </h2>
            
            <div class="bg-white rounded-xl shadow-lg p-8">
                <p class="text-gray-700 mb-6 leading-relaxed">
                    SpringApplication 类的构造方法主要有以下几种重载形式。通常，启动 Spring Boot 应用时会用到 run(String... args) 方法。
                </p>
                
                <div class="code-block mb-6">
                    <pre>public class SpringApplication {
    private static final Log logger = LogFactory.getLog(SpringApplication.class);
    
    private final Class<?> primarySource;
    private final Set<Class<?>> sources = new LinkedHashSet<>();
    private ApplicationContext context;
    
    public SpringApplication(Class<?> primarySource) {
        Assert.notNull(primarySource, "Primary source must not be null");
        this.primarySource = primarySource;
        this.sources.add(primarySource);
    }
    
    public SpringApplication(Class<?>... primarySources) {
        Assert.notNull(primarySources, "Primary sources must not be null");
        this.primarySource = null;
        this.sources.addAll(Arrays.asList(primarySources));
    }
}</pre>
                </div>
                
                <div class="highlight-box">
                    <h4 class="font-semibold text-gray-800 mb-2">
                        <i class="fas fa-info-circle mr-2"></i>构造器作用说明
                    </h4>
                    <p class="text-gray-600">
                        构造方法接收一个或多个类作为参数（如 @SpringBootApplication 注解的主类）。这些类作为应用的主要源，将会用于构建应用上下文。
                    </p>
                </div>
            </div>
        </section>

        <!-- Run Method Analysis -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <i class="fas fa-play-circle mr-3 text-purple-600"></i>
                <span class="gradient-text">run() 方法源码分析</span>
            </h2>
            
            <div class="bg-white rounded-xl shadow-lg p-8">
                <p class="text-gray-700 mb-6 leading-relaxed">
                    run() 方法是 SpringApplication 启动应用的入口。它负责启动 Spring 容器并处理传入的参数。
                </p>
                
                <div class="code-block mb-8">
                    <pre>public ConfigurableApplicationContext run(String... args) {
    // 1. 解析命令行参数并初始化默认的环境
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    
    // 2. 设置环境变量，加载配置
    ApplicationEnvironment environment = prepareEnvironment(listeners, args);
    
    // 3. 创建应用上下文
    ConfigurableApplicationContext context = createApplicationContext();
    
    // 4. 准备上下文
    prepareContext(context, environment, listeners, args);
    
    // 5. 启动上下文
    refreshContext(context);
    
    // 6. 执行所有监听器（可用于扩展或执行启动逻辑）
    afterRefresh(context, args);
    
    // 7. 完成，返回应用上下文
    return context;
}</pre>
                </div>

                <!-- Process Flow Diagram -->
                <div class="mermaid">
                    graph TD
                        A[SpringApplication.run] --> B[获取运行监听器]
                        B --> C[准备环境配置]
                        C --> D[创建应用上下文]
                        D --> E[准备上下文]
                        E --> F[刷新上下文]
                        F --> G[执行后续操作]
                        G --> H[返回应用上下文]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style H fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </section>

        <!-- Detailed Steps -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <i class="fas fa-list-ol mr-3 text-purple-600"></i>
                <span class="gradient-text">启动流程详细步骤</span>
            </h2>
            
            <div class="space-y-6">
                <!-- Step 1 -->
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-3 text-purple-700">
                        <i class="fas fa-headphones mr-2"></i>1. 解析命令行参数并初始化环境
                    </h3>
                    <div class="code-block mb-4">
                        <pre>SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();</pre>
                    </div>
                    <p class="text-gray-600">
                        getRunListeners(args) 返回一组监听器，这些监听器会在应用启动过程中参与执行。listeners.starting() 通知监听器应用开始启动。
                    </p>
                </div>

                <!-- Step 2 -->
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-3 text-purple-700">
                        <i class="fas fa-cog mr-2"></i>2. 设置环境变量，加载配置
                    </h3>
                    <div class="code-block mb-4">
                        <pre>ApplicationEnvironment environment = prepareEnvironment(listeners, args);</pre>
                    </div>
                    <p class="text-gray-600">
                        prepareEnvironment() 方法用于创建和初始化 Spring 环境。它会设置应用的配置源，包括环境变量、系统属性、配置文件等。
                    </p>
                </div>

                <!-- Step 3 -->
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-3 text-purple-700">
                        <i class="fas fa-box mr-2"></i>3. 创建应用上下文
                    </h3>
                    <div class="code-block mb-4">
                        <pre>ConfigurableApplicationContext context = createApplicationContext();</pre>
                    </div>
                    <p class="text